package com.example.springdemo.Controller;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alipay.api.AlipayApiException;
import com.example.springdemo.entity.Address;
import com.example.springdemo.entity.Order;
import com.example.springdemo.entity.Orderdetail;
import com.example.springdemo.pojo.AlipayBean;
import com.example.springdemo.service.AlipayService;
import com.example.springdemo.service.AlipayServiceImpl;
import com.example.springdemo.service.IOrderService;
import com.example.springdemo.utils.PassToken;
import com.example.springdemo.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * orderController
 * 
 * @author ruoyi
 * @date 2025-05-07
 */
@RestController
@RequestMapping("/system/order")
public class OrderController
{
    @Autowired
 private IOrderService orderService;
    /**
     * 查询order列表
     */
    @PassToken
    @GetMapping("/list")
    public ResponseResult list(Order order)
    {
//        startPage();
//        List<Order> list = orderService.selectOrderList(order);
//        return getDataTable(list);
        return ResponseResult.createBySuccess(200,orderService.selectOrderList(order),"查询成功");
    }
    @PassToken
    @GetMapping("/frontlist")
    public ResponseResult list1(Order order) {
        System.out.println(order.getOrderid());
        return ResponseResult.createBySuccess(200,order,"获取成功");
    }

    /**
     * 导出order列表
     */
//    @Log(title = "order", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, Order order)
//    {
//        List<Order> list = orderService.selectOrderList(order);
//        ExcelUtil<Order> util = new ExcelUtil<Order>(Order.class);
//        util.exportExcel(response, list, "order数据");
//    }

    @PassToken
    @GetMapping(value="/order/{ordersn}")
    public ResponseResult getOrder(@PathVariable("ordersn") String ordersn){
        return ResponseResult.createBySuccess(200,orderService.getOrderByOrdersn(ordersn),"获取成功");
    }

    /**
     * 获取order详细信息
     */
@PassToken
    @GetMapping(value = "/{orderid}")
    public ResponseResult getInfo(@PathVariable("orderid") Long orderid)
    {
        Order order = orderService.selectOrderByOrderid(orderid);
        return ResponseResult.createBySuccess(200,orderid,"查询成功");
    }

    /**
     * 新增order
     */

   @PassToken
    @PostMapping
    public ResponseResult add(@RequestBody Order order)
    {
//        String ordersn = UUID.randomUUID()+"";
//        order.setOrdersn(ordersn);
//        List<Orderdetail> list = order.getOrderdetailList();
//        for(Orderdetail od:list){
//            od.setOrdersn(ordersn);
//        }
//产生时间戳
        long timestamp = System.currentTimeMillis();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        sdf.setTimeZone(TimeZone.getTimeZone("GMT-8"));
        String formattedDate = sdf.format(new Date(timestamp));
        System.out.println("北京时间：" + formattedDate);
        order.setOrdersn(formattedDate);
        List<Orderdetail> list = order.getOrderdetailList();
        for(Orderdetail od:list){
            od.setOrdersn(formattedDate);
        }



        System.out.println(order.getOrdersn());
        System.out.println(order.getOrderdetailList().size());
        int i = orderService.insertOrder(order);
        if (i > 0) {
            return ResponseResult.createBySuccess(200, order.getOrdersn(), "添加成功");
        } else {
            return ResponseResult.createByFail(200, "添加失败");
        }
    }

    /**
     * 修改order
     */

  @PassToken
    @PutMapping
    public ResponseResult edit(@RequestBody Order order)
    {
        int i = orderService.updateOrder(order);
        if (i > 0) {
            return ResponseResult.createBySuccess(200, "", "修改成功");
        } else {
            return ResponseResult.createByFail(200, "修改失败");
        }
    }

    /**
     * 删除order
     */

   @PassToken
	@DeleteMapping("/{orderids}")
    public ResponseResult remove(@PathVariable Long[] orderids)
    {
        int i = orderService.deleteOrderByOrderids(orderids);
        if (i > 0) {
            return ResponseResult.createBySuccess(200, "", "删除成功");
        } else {
            return ResponseResult.createByFail(200, "删除失败");
        }
    }

    @Autowired
    AlipayService alipayService;
   @PassToken
   @GetMapping("/pay")
    public String payOrder(String outTradeNo,String subject,String totalAmount,String body,HttpServletResponse response)
       throws AlipayApiException, IOException{
       AlipayBean alipayBean = new AlipayBean();
       alipayBean.setOut_trade_no(outTradeNo);
       alipayBean.setSubject(subject);
       alipayBean.setTotal_amount(totalAmount);
       alipayBean.setBody(body);
       System.out.println(outTradeNo+subject+" "+alipayBean.getOut_trade_no());

       return alipayService.aliPay(alipayBean);
   }

@PassToken
@GetMapping("/alipay_back")
public String alipayback(@RequestParam Map<String,String> params, HttpServletRequest request){
    String orderNo = params.get("out_trade_no");
    String tradeNo = params.get("trade_no");
    String tradeStatus = params.get("trade_status");
    System.out.println(orderNo+"  "+tradeNo+"  "+tradeStatus);
//    Orders o = new Orders();
//    o.setOrdersn(orderNo);
    Order order = orderService.getOrderByOrdersn(orderNo);
            if(order!=null){
                order.setState(new Long(1));
                int i = orderService.updateOrder(order);
                System.out.println(i+"  ");
                //商品
            }
            return "redirect:http://localhost:8080/";
}
}